<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 6.3.0">
<link rel="preconnect" href="https://fonts.googleapis.com" crossorigin>
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,300italic,400,400italic,700,700italic%7CZCOOL+QingKe+HuangYou:300,300italic,400,400italic,700,700italic%7CNoto+Sans+Simplified+Chinese:300,300italic,400,400italic,700,700italic%7CJetBrains+Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="/lib/@fortawesome/fontawesome-free/css/all.min.css" integrity="sha256-HtsXJanqjKTc8vVQjO4YMhiqFoXkfBsjBWcX91T1jr8=" crossorigin="anonymous">
  <link rel="stylesheet" href="/lib/animate.css/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="/lib/@fancyapps/ui/dist/fancybox/fancybox.css" integrity="sha256-RvRHGSuWAxZpXKV9lLDt2e+rZ+btzn48Wp4ueS3NZKs=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"wyatex.online","root":"/","images":"/images","scheme":"Pisces","darkmode":true,"version":"8.18.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":false,"height":500},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":true}}</script><script src="/js/config.js"></script>

    <meta name="description" content="一些JavaScript的代码">
<meta property="og:type" content="article">
<meta property="og:title" content="JS常用60余种工具方法">
<meta property="og:url" content="https://wyatex.online/%E5%89%8D%E7%AB%AF/%E5%89%8D%E7%AB%AF%E5%B8%B8%E7%94%A860%E4%BD%99%E7%A7%8D%E5%B7%A5%E5%85%B7%E6%96%B9%E6%B3%95/index.html">
<meta property="og:site_name" content="Wyatex&#96;s blog">
<meta property="og:description" content="一些JavaScript的代码">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-05-28T14:54:44.000Z">
<meta property="article:modified_time" content="2023-05-05T01:25:47.442Z">
<meta property="article:author" content="Wyatex">
<meta property="article:tag" content="JavaScript">
<meta property="article:tag" content="前端">
<meta property="article:tag" content="工具">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://wyatex.online/%E5%89%8D%E7%AB%AF/%E5%89%8D%E7%AB%AF%E5%B8%B8%E7%94%A860%E4%BD%99%E7%A7%8D%E5%B7%A5%E5%85%B7%E6%96%B9%E6%B3%95/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://wyatex.online/%E5%89%8D%E7%AB%AF/%E5%89%8D%E7%AB%AF%E5%B8%B8%E7%94%A860%E4%BD%99%E7%A7%8D%E5%B7%A5%E5%85%B7%E6%96%B9%E6%B3%95/","path":"前端/前端常用60余种工具方法/","title":"JS常用60余种工具方法"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>JS常用60余种工具方法 | Wyatex`s blog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Wyatex`s blog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">97</span></a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">26</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">155</span></a></li><li class="menu-item menu-item-编程"><a href="/programming/" rel="section"><i class="fa fa-book fa-fw"></i>编程</a></li><li class="menu-item menu-item-蹲坑读物"><a href="/%E5%B9%B2%E8%B4%A7/%E5%BC%80%E5%8F%91%E4%BA%A4%E6%B5%81%E7%BE%A4%E5%88%86%E4%BA%AB%E8%AE%B0%E5%BD%95/" rel="section"><i class="fa fa-book fa-fw"></i>蹲坑读物</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-overview-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Wyatex"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Wyatex</p>
  <div class="site-description" itemprop="description">这里是我用来做笔记的网站哟~</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">155</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">26</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">97</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/wyatex" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wyatex" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:wyatex@qq.com" title="E-Mail → mailto:wyatex@qq.com" rel="noopener me" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
    <div class="sidebar-inner sidebar-blogroll">
      <div class="links-of-blogroll animated">
        <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
          链接
        </div>
        <ul class="links-of-blogroll-list">
            <li class="links-of-blogroll-item">
              <a href="https://antfu.me/" title="https:&#x2F;&#x2F;antfu.me&#x2F;" rel="noopener" target="_blank">Anthony Fu</a>
            </li>
            <li class="links-of-blogroll-item">
              <a href="https://www.codesky.me/" title="https:&#x2F;&#x2F;www.codesky.me&#x2F;" rel="noopener" target="_blank">CodeSky</a>
            </li>
            <li class="links-of-blogroll-item">
              <a href="https://learn.lianglianglee.com/" title="https:&#x2F;&#x2F;learn.lianglianglee.com&#x2F;" rel="noopener" target="_blank">技术文章摘抄</a>
            </li>
        </ul>
      </div>
    </div>
        <div class="pjax">
        </div>
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://wyatex.online/%E5%89%8D%E7%AB%AF/%E5%89%8D%E7%AB%AF%E5%B8%B8%E7%94%A860%E4%BD%99%E7%A7%8D%E5%B7%A5%E5%85%B7%E6%96%B9%E6%B3%95/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Wyatex">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Wyatex`s blog">
      <meta itemprop="description" content="这里是我用来做笔记的网站哟~">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="JS常用60余种工具方法 | Wyatex`s blog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JS常用60余种工具方法
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-05-28 22:54:44" itemprop="dateCreated datePublished" datetime="2020-05-28T22:54:44+08:00">2020-05-28</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-05-05 09:25:47" itemprop="dateModified" datetime="2023-05-05T09:25:47+08:00">2023-05-05</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">前端</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>一些JavaScript的代码</p>
<span id="more"></span>

<p>1.邮箱</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isEmail</span> = (<span class="params">s</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="regexp">/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]&#123;2,3&#125;)&#123;1,2&#125;)$/</span>.<span class="title function_">test</span>(s)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>2.手机号码</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isMobile</span> = (<span class="params">s</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="regexp">/^1[0-9]&#123;10&#125;$/</span>.<span class="title function_">test</span>(s)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>3.电话号码</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isPhone</span> = (<span class="params">s</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="regexp">/^([0-9]&#123;3,4&#125;-)?[0-9]&#123;7,8&#125;$/</span>.<span class="title function_">test</span>(s)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>4.是否url地址</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isURL</span> = (<span class="params">s</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="regexp">/^http[s]?:\/\/.*/</span>.<span class="title function_">test</span>(s)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>5.是否字符串</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isString</span> = (<span class="params">o</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">toString</span>.<span class="title function_">call</span>(o).<span class="title function_">slice</span>(<span class="number">8</span>, -<span class="number">1</span>) === <span class="string">&#x27;String&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>6.是否数字</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isNumber</span> = (<span class="params">o</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">toString</span>.<span class="title function_">call</span>(o).<span class="title function_">slice</span>(<span class="number">8</span>, -<span class="number">1</span>) === <span class="string">&#x27;Number&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>7.是否boolean</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isBoolean</span> = (<span class="params">o</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">toString</span>.<span class="title function_">call</span>(o).<span class="title function_">slice</span>(<span class="number">8</span>, -<span class="number">1</span>) === <span class="string">&#x27;Boolean&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>8.是否函数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isFunction</span> = (<span class="params">o</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">toString</span>.<span class="title function_">call</span>(o).<span class="title function_">slice</span>(<span class="number">8</span>, -<span class="number">1</span>) === <span class="string">&#x27;Function&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>9.是否为null</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isNull</span> = (<span class="params">o</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">toString</span>.<span class="title function_">call</span>(o).<span class="title function_">slice</span>(<span class="number">8</span>, -<span class="number">1</span>) === <span class="string">&#x27;Null&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>10.是否undefined</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isUndefined</span> = (<span class="params">o</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">toString</span>.<span class="title function_">call</span>(o).<span class="title function_">slice</span>(<span class="number">8</span>, -<span class="number">1</span>) === <span class="string">&#x27;Undefined&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>11.是否对象</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isObj</span> = (<span class="params">o</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">toString</span>.<span class="title function_">call</span>(o).<span class="title function_">slice</span>(<span class="number">8</span>, -<span class="number">1</span>) === <span class="string">&#x27;Object&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>12.是否数组</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isArray</span> = (<span class="params">o</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">toString</span>.<span class="title function_">call</span>(o).<span class="title function_">slice</span>(<span class="number">8</span>, -<span class="number">1</span>) === <span class="string">&#x27;Array&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>13.是否时间</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isDate</span> = (<span class="params">o</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">toString</span>.<span class="title function_">call</span>(o).<span class="title function_">slice</span>(<span class="number">8</span>, -<span class="number">1</span>) === <span class="string">&#x27;Date&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>14.是否正则</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isRegExp</span> = (<span class="params">o</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">toString</span>.<span class="title function_">call</span>(o).<span class="title function_">slice</span>(<span class="number">8</span>, -<span class="number">1</span>) === <span class="string">&#x27;RegExp&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>15.是否错误对象</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isError</span> = (<span class="params">o</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">toString</span>.<span class="title function_">call</span>(o).<span class="title function_">slice</span>(<span class="number">8</span>, -<span class="number">1</span>) === <span class="string">&#x27;Error&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>16.是否Symbol函数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isSymbol</span> = (<span class="params">o</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">toString</span>.<span class="title function_">call</span>(o).<span class="title function_">slice</span>(<span class="number">8</span>, -<span class="number">1</span>) === <span class="string">&#x27;Symbol&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>17.是否Promise对象</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isPromise</span> = (<span class="params">o</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">toString</span>.<span class="title function_">call</span>(o).<span class="title function_">slice</span>(<span class="number">8</span>, -<span class="number">1</span>) === <span class="string">&#x27;Promise&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>18.是否Set对象</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isSet</span> = (<span class="params">o</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">toString</span>.<span class="title function_">call</span>(o).<span class="title function_">slice</span>(<span class="number">8</span>, -<span class="number">1</span>) === <span class="string">&#x27;Set&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> ua = navigator.<span class="property">userAgent</span>.<span class="title function_">toLowerCase</span>();</span><br></pre></td></tr></table></figure>
<p>19.是否是微信浏览器</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isWeiXin</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> ua.<span class="title function_">match</span>(<span class="regexp">/microMessenger/i</span>) == <span class="string">&#x27;micromessenger&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>20.是否是移动端</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isDeviceMobile</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="regexp">/android|webos|iphone|ipod|balckberry/i</span>.<span class="title function_">test</span>(ua)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>21.是否是QQ浏览器</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isQQBrowser</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> !!ua.<span class="title function_">match</span>(<span class="regexp">/mqqbrowser|qzone|qqbrowser|qbwebviewtype/i</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>22.是否是爬虫</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isSpider</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="regexp">/adsbot|googlebot|bingbot|msnbot|yandexbot|baidubot|robot|careerbot|seznambot|bot|baiduspider|jikespider|symantecspider|scannerlwebcrawler|crawler|360spider|sosospider|sogou web sprider|sogou orion spider/</span>.<span class="title function_">test</span>(ua)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>23.是否ios</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isIos</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">var</span> u = navigator.<span class="property">userAgent</span>;</span><br><span class="line">    <span class="keyword">if</span> (u.<span class="title function_">indexOf</span>(<span class="string">&#x27;Android&#x27;</span>) &gt; -<span class="number">1</span> || u.<span class="title function_">indexOf</span>(<span class="string">&#x27;Linux&#x27;</span>) &gt; -<span class="number">1</span>) &#123;  <span class="comment">//安卓手机</span></span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (u.<span class="title function_">indexOf</span>(<span class="string">&#x27;iPhone&#x27;</span>) &gt; -<span class="number">1</span>) &#123;<span class="comment">//苹果手机</span></span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (u.<span class="title function_">indexOf</span>(<span class="string">&#x27;iPad&#x27;</span>) &gt; -<span class="number">1</span>) &#123;<span class="comment">//iPad</span></span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (u.<span class="title function_">indexOf</span>(<span class="string">&#x27;Windows Phone&#x27;</span>) &gt; -<span class="number">1</span>) &#123;<span class="comment">//winphone手机</span></span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>24.是否为PC端</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isPC</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">var</span> userAgentInfo = navigator.<span class="property">userAgent</span>;</span><br><span class="line">    <span class="keyword">var</span> <span class="title class_">Agents</span> = [<span class="string">&quot;Android&quot;</span>, <span class="string">&quot;iPhone&quot;</span>,</span><br><span class="line">        <span class="string">&quot;SymbianOS&quot;</span>, <span class="string">&quot;Windows Phone&quot;</span>,</span><br><span class="line">        <span class="string">&quot;iPad&quot;</span>, <span class="string">&quot;iPod&quot;</span>];</span><br><span class="line">    <span class="keyword">var</span> flag = <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> v = <span class="number">0</span>; v &lt; <span class="title class_">Agents</span>.<span class="property">length</span>; v++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (userAgentInfo.<span class="title function_">indexOf</span>(<span class="title class_">Agents</span>[v]) &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            flag = <span class="literal">false</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> flag;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>25.去除html标签</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">removeHtmltag</span> = (<span class="params">str</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> str.<span class="title function_">replace</span>(<span class="regexp">/&lt;[^&gt;]+&gt;/g</span>, <span class="string">&#x27;&#x27;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>26.获取url参数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">getQueryString</span> = (<span class="params">name</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> reg = <span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&#x27;(^|&amp;)&#x27;</span> + name + <span class="string">&#x27;=([^&amp;]*)(&amp;|$)&#x27;</span>, <span class="string">&#x27;i&#x27;</span>);</span><br><span class="line">    <span class="keyword">const</span> search = <span class="variable language_">window</span>.<span class="property">location</span>.<span class="property">search</span>.<span class="title function_">split</span>(<span class="string">&#x27;?&#x27;</span>)[<span class="number">1</span>] || <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">    <span class="keyword">const</span> r = search.<span class="title function_">match</span>(reg) || [];</span><br><span class="line">    <span class="keyword">return</span> r[<span class="number">2</span>];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>27.动态引入js</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">injectScript</span> = (<span class="params">src</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> s = <span class="variable language_">document</span>.<span class="title function_">createElement</span>(<span class="string">&#x27;script&#x27;</span>);</span><br><span class="line">    s.<span class="property">type</span> = <span class="string">&#x27;text/javascript&#x27;</span>;</span><br><span class="line">    s.<span class="property">async</span> = <span class="literal">true</span>;</span><br><span class="line">    s.<span class="property">src</span> = src;</span><br><span class="line">    <span class="keyword">const</span> t = <span class="variable language_">document</span>.<span class="title function_">getElementsByTagName</span>(<span class="string">&#x27;script&#x27;</span>)[<span class="number">0</span>];</span><br><span class="line">    t.<span class="property">parentNode</span>.<span class="title function_">insertBefore</span>(s, t);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>28.根据url地址下载</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">download</span> = (<span class="params">url</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">var</span> isChrome = navigator.<span class="property">userAgent</span>.<span class="title function_">toLowerCase</span>().<span class="title function_">indexOf</span>(<span class="string">&#x27;chrome&#x27;</span>) &gt; -<span class="number">1</span>;</span><br><span class="line">    <span class="keyword">var</span> isSafari = navigator.<span class="property">userAgent</span>.<span class="title function_">toLowerCase</span>().<span class="title function_">indexOf</span>(<span class="string">&#x27;safari&#x27;</span>) &gt; -<span class="number">1</span>;</span><br><span class="line">    <span class="keyword">if</span> (isChrome || isSafari) &#123;</span><br><span class="line">        <span class="keyword">var</span> link = <span class="variable language_">document</span>.<span class="title function_">createElement</span>(<span class="string">&#x27;a&#x27;</span>);</span><br><span class="line">        link.<span class="property">href</span> = url;</span><br><span class="line">        <span class="keyword">if</span> (link.<span class="property">download</span> !== <span class="literal">undefined</span>) &#123;</span><br><span class="line">            <span class="keyword">var</span> fileName = url.<span class="title function_">substring</span>(url.<span class="title function_">lastIndexOf</span>(<span class="string">&#x27;/&#x27;</span>) + <span class="number">1</span>, url.<span class="property">length</span>);</span><br><span class="line">            link.<span class="property">download</span> = fileName;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (<span class="variable language_">document</span>.<span class="property">createEvent</span>) &#123;</span><br><span class="line">            <span class="keyword">var</span> e = <span class="variable language_">document</span>.<span class="title function_">createEvent</span>(<span class="string">&#x27;MouseEvents&#x27;</span>);</span><br><span class="line">            e.<span class="title function_">initEvent</span>(<span class="string">&#x27;click&#x27;</span>, <span class="literal">true</span>, <span class="literal">true</span>);</span><br><span class="line">            link.<span class="title function_">dispatchEvent</span>(e);</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (url.<span class="title function_">indexOf</span>(<span class="string">&#x27;?&#x27;</span>) === -<span class="number">1</span>) &#123;</span><br><span class="line">        url += <span class="string">&#x27;?download&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="variable language_">window</span>.<span class="title function_">open</span>(url, <span class="string">&#x27;_self&#x27;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>29.el是否包含某个class</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">hasClass</span> = (<span class="params">el, className</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">let</span> reg = <span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&#x27;(^|\\s)&#x27;</span> + className + <span class="string">&#x27;(\\s|$)&#x27;</span>)</span><br><span class="line">    <span class="keyword">return</span> reg.<span class="title function_">test</span>(el.<span class="property">className</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>30.el添加某个class</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">addClass</span> = (<span class="params">el, className</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="title function_">hasClass</span>(el, className)) &#123;</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">let</span> newClass = el.<span class="property">className</span>.<span class="title function_">split</span>(<span class="string">&#x27; &#x27;</span>)</span><br><span class="line">    newClass.<span class="title function_">push</span>(className)</span><br><span class="line">    el.<span class="property">className</span> = newClass.<span class="title function_">join</span>(<span class="string">&#x27; &#x27;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>31.el去除某个class</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">removeClass</span> = (<span class="params">el, className</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">if</span> (!<span class="title function_">hasClass</span>(el, className)) &#123;</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">let</span> reg = <span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&#x27;(^|\\s)&#x27;</span> + className + <span class="string">&#x27;(\\s|$)&#x27;</span>, <span class="string">&#x27;g&#x27;</span>)</span><br><span class="line">    el.<span class="property">className</span> = el.<span class="property">className</span>.<span class="title function_">replace</span>(reg, <span class="string">&#x27; &#x27;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>32.获取滚动的坐标</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">getScrollPosition</span> = (<span class="params">el = <span class="variable language_">window</span></span>) =&gt; (&#123;</span><br><span class="line">    <span class="attr">x</span>: el.<span class="property">pageXOffset</span> !== <span class="literal">undefined</span> ? el.<span class="property">pageXOffset</span> : el.<span class="property">scrollLeft</span>,</span><br><span class="line">    <span class="attr">y</span>: el.<span class="property">pageYOffset</span> !== <span class="literal">undefined</span> ? el.<span class="property">pageYOffset</span> : el.<span class="property">scrollTop</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>33.滚动到顶部</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">scrollToTop</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> c = <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">scrollTop</span> || <span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">scrollTop</span>;</span><br><span class="line">    <span class="keyword">if</span> (c &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="variable language_">window</span>.<span class="title function_">requestAnimationFrame</span>(scrollToTop);</span><br><span class="line">        <span class="variable language_">window</span>.<span class="title function_">scrollTo</span>(<span class="number">0</span>, c - c / <span class="number">8</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>34.el是否在视口范围内</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">elementIsVisibleInViewport</span> = (<span class="params">el, partiallyVisible = <span class="literal">false</span></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; top, left, bottom, right &#125; = el.<span class="title function_">getBoundingClientRect</span>();</span><br><span class="line">    <span class="keyword">const</span> &#123; innerHeight, innerWidth &#125; = <span class="variable language_">window</span>;</span><br><span class="line">    <span class="keyword">return</span> partiallyVisible</span><br><span class="line">        ? ((top &gt; <span class="number">0</span> &amp;&amp; top &lt; innerHeight) || (bottom &gt; <span class="number">0</span> &amp;&amp; bottom &lt; innerHeight)) &amp;&amp;</span><br><span class="line">        ((left &gt; <span class="number">0</span> &amp;&amp; left &lt; innerWidth) || (right &gt; <span class="number">0</span> &amp;&amp; right &lt; innerWidth))</span><br><span class="line">        : top &gt;= <span class="number">0</span> &amp;&amp; left &gt;= <span class="number">0</span> &amp;&amp; bottom &lt;= innerHeight &amp;&amp; right &lt;= innerWidth;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>35.洗牌算法随机</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">shuffle</span> = (<span class="params">arr</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">var</span> result = [],</span><br><span class="line">        random;</span><br><span class="line">    <span class="keyword">while</span> (arr.<span class="property">length</span> &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        random = <span class="title class_">Math</span>.<span class="title function_">floor</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * arr.<span class="property">length</span>);</span><br><span class="line">        result.<span class="title function_">push</span>(arr[random])</span><br><span class="line">        arr.<span class="title function_">splice</span>(random, <span class="number">1</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>36.劫持粘贴板</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">copyTextToClipboard</span> = (<span class="params">value</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">var</span> textArea = <span class="variable language_">document</span>.<span class="title function_">createElement</span>(<span class="string">&quot;textarea&quot;</span>);</span><br><span class="line">    textArea.<span class="property">style</span>.<span class="property">background</span> = <span class="string">&#x27;transparent&#x27;</span>;</span><br><span class="line">    textArea.<span class="property">value</span> = value;</span><br><span class="line">    <span class="variable language_">document</span>.<span class="property">body</span>.<span class="title function_">appendChild</span>(textArea);</span><br><span class="line">    textArea.<span class="title function_">select</span>();</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="keyword">var</span> successful = <span class="variable language_">document</span>.<span class="title function_">execCommand</span>(<span class="string">&#x27;copy&#x27;</span>);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (err) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;Oops, unable to copy&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="variable language_">document</span>.<span class="property">body</span>.<span class="title function_">removeChild</span>(textArea);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>37.判断类型集合</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">checkStr</span> = (<span class="params">str, type</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">switch</span> (type) &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;phone&#x27;</span>:   <span class="comment">//手机号码</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^1[3|4|5|6|7|8|9][0-9]&#123;9&#125;$/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;tel&#x27;</span>:     <span class="comment">//座机</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^(0\d&#123;2,3&#125;-\d&#123;7,8&#125;)(-\d&#123;1,4&#125;)?$/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;card&#x27;</span>:    <span class="comment">//身份证</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/(^\d&#123;15&#125;$)|(^\d&#123;18&#125;$)|(^\d&#123;17&#125;(\d|X|x)$)/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;pwd&#x27;</span>:     <span class="comment">//密码以字母开头，长度在6~18之间，只能包含字母、数字和下划线</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^[a-zA-Z]\w&#123;5,17&#125;$/</span>.<span class="title function_">test</span>(str)</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;postal&#x27;</span>:  <span class="comment">//邮政编码</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/[1-9]\d&#123;5&#125;(?!\d)/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;QQ&#x27;</span>:      <span class="comment">//QQ号</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^[1-9][0-9]&#123;4,9&#125;$/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;email&#x27;</span>:   <span class="comment">//邮箱</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;money&#x27;</span>:   <span class="comment">//金额(小数点2位)</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^\d*(?:\.\d&#123;0,2&#125;)?$/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;URL&#x27;</span>:     <span class="comment">//网址</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?/</span>.<span class="title function_">test</span>(str)</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;IP&#x27;</span>:      <span class="comment">//IP</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.)&#123;3&#125;(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;date&#x27;</span>:    <span class="comment">//日期时间</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^(\d&#123;4&#125;)\-(\d&#123;2&#125;)\-(\d&#123;2&#125;) (\d&#123;2&#125;)(?:\:\d&#123;2&#125;|:(\d&#123;2&#125;):(\d&#123;2&#125;))$/</span>.<span class="title function_">test</span>(str) || <span class="regexp">/^(\d&#123;4&#125;)\-(\d&#123;2&#125;)\-(\d&#123;2&#125;)$/</span>.<span class="title function_">test</span>(str)</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;number&#x27;</span>:  <span class="comment">//数字</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^[0-9]$/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;english&#x27;</span>: <span class="comment">//英文</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^[a-zA-Z]+$/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;chinese&#x27;</span>: <span class="comment">//中文</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^[\\u4E00-\\u9FA5]+$/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;lower&#x27;</span>:   <span class="comment">//小写</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^[a-z]+$/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;upper&#x27;</span>:   <span class="comment">//大写</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/^[A-Z]+$/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;HTML&#x27;</span>:    <span class="comment">//HTML标记</span></span><br><span class="line">            <span class="keyword">return</span> <span class="regexp">/&lt;(&quot;[^&quot;]*&quot;|&#x27;[^&#x27;]*&#x27;|[^&#x27;&quot;&gt;])*&gt;/</span>.<span class="title function_">test</span>(str);</span><br><span class="line">        <span class="attr">default</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>38.严格的身份证校验</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isCardID</span> = (<span class="params">sId</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">if</span> (!<span class="regexp">/(^\d&#123;15&#125;$)|(^\d&#123;17&#125;(\d|X|x)$)/</span>.<span class="title function_">test</span>(sId)) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;你输入的身份证长度或格式错误&#x27;</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//身份证城市</span></span><br><span class="line">    <span class="keyword">var</span> aCity = &#123; <span class="number">11</span>: <span class="string">&quot;北京&quot;</span>, <span class="number">12</span>: <span class="string">&quot;天津&quot;</span>, <span class="number">13</span>: <span class="string">&quot;河北&quot;</span>, <span class="number">14</span>: <span class="string">&quot;山西&quot;</span>, <span class="number">15</span>: <span class="string">&quot;内蒙古&quot;</span>, <span class="number">21</span>: <span class="string">&quot;辽宁&quot;</span>, <span class="number">22</span>: <span class="string">&quot;吉林&quot;</span>, <span class="number">23</span>: <span class="string">&quot;黑龙江&quot;</span>, <span class="number">31</span>: <span class="string">&quot;上海&quot;</span>, <span class="number">32</span>: <span class="string">&quot;江苏&quot;</span>, <span class="number">33</span>: <span class="string">&quot;浙江&quot;</span>, <span class="number">34</span>: <span class="string">&quot;安徽&quot;</span>, <span class="number">35</span>: <span class="string">&quot;福建&quot;</span>, <span class="number">36</span>: <span class="string">&quot;江西&quot;</span>, <span class="number">37</span>: <span class="string">&quot;山东&quot;</span>, <span class="number">41</span>: <span class="string">&quot;河南&quot;</span>, <span class="number">42</span>: <span class="string">&quot;湖北&quot;</span>, <span class="number">43</span>: <span class="string">&quot;湖南&quot;</span>, <span class="number">44</span>: <span class="string">&quot;广东&quot;</span>, <span class="number">45</span>: <span class="string">&quot;广西&quot;</span>, <span class="number">46</span>: <span class="string">&quot;海南&quot;</span>, <span class="number">50</span>: <span class="string">&quot;重庆&quot;</span>, <span class="number">51</span>: <span class="string">&quot;四川&quot;</span>, <span class="number">52</span>: <span class="string">&quot;贵州&quot;</span>, <span class="number">53</span>: <span class="string">&quot;云南&quot;</span>, <span class="number">54</span>: <span class="string">&quot;西藏&quot;</span>, <span class="number">61</span>: <span class="string">&quot;陕西&quot;</span>, <span class="number">62</span>: <span class="string">&quot;甘肃&quot;</span>, <span class="number">63</span>: <span class="string">&quot;青海&quot;</span>, <span class="number">64</span>: <span class="string">&quot;宁夏&quot;</span>, <span class="number">65</span>: <span class="string">&quot;新疆&quot;</span>, <span class="number">71</span>: <span class="string">&quot;台湾&quot;</span>, <span class="number">81</span>: <span class="string">&quot;香港&quot;</span>, <span class="number">82</span>: <span class="string">&quot;澳门&quot;</span>, <span class="number">91</span>: <span class="string">&quot;国外&quot;</span> &#125;;</span><br><span class="line">    <span class="keyword">if</span> (!aCity[<span class="built_in">parseInt</span>(sId.<span class="title function_">substr</span>(<span class="number">0</span>, <span class="number">2</span>))]) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;你的身份证地区非法&#x27;</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 出生日期验证</span></span><br><span class="line">    <span class="keyword">var</span> sBirthday = (sId.<span class="title function_">substr</span>(<span class="number">6</span>, <span class="number">4</span>) + <span class="string">&quot;-&quot;</span> + <span class="title class_">Number</span>(sId.<span class="title function_">substr</span>(<span class="number">10</span>, <span class="number">2</span>)) + <span class="string">&quot;-&quot;</span> + <span class="title class_">Number</span>(sId.<span class="title function_">substr</span>(<span class="number">12</span>, <span class="number">2</span>))).<span class="title function_">replace</span>(<span class="regexp">/-/g</span>, <span class="string">&quot;/&quot;</span>),</span><br><span class="line">        d = <span class="keyword">new</span> <span class="title class_">Date</span>(sBirthday)</span><br><span class="line">    <span class="keyword">if</span> (sBirthday != (d.<span class="title function_">getFullYear</span>() + <span class="string">&quot;/&quot;</span> + (d.<span class="title function_">getMonth</span>() + <span class="number">1</span>) + <span class="string">&quot;/&quot;</span> + d.<span class="title function_">getDate</span>())) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;身份证上的出生日期非法&#x27;</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 身份证号码校验</span></span><br><span class="line">    <span class="keyword">var</span> sum = <span class="number">0</span>,</span><br><span class="line">        weights = [<span class="number">7</span>, <span class="number">9</span>, <span class="number">10</span>, <span class="number">5</span>, <span class="number">8</span>, <span class="number">4</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">6</span>, <span class="number">3</span>, <span class="number">7</span>, <span class="number">9</span>, <span class="number">10</span>, <span class="number">5</span>, <span class="number">8</span>, <span class="number">4</span>, <span class="number">2</span>],</span><br><span class="line">        codes = <span class="string">&quot;10X98765432&quot;</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; sId.<span class="property">length</span> - <span class="number">1</span>; i++) &#123;</span><br><span class="line">        sum += sId[i] * weights[i];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> last = codes[sum % <span class="number">11</span>]; <span class="comment">//计算出来的最后一位身份证号码</span></span><br><span class="line">    <span class="keyword">if</span> (sId[sId.<span class="property">length</span> - <span class="number">1</span>] != last) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;你输入的身份证号非法&#x27;</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>39.随机数范围</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">random</span> = (<span class="params">min, max</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">arguments</span>.<span class="property">length</span> === <span class="number">2</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="title class_">Math</span>.<span class="title function_">floor</span>(min + <span class="title class_">Math</span>.<span class="title function_">random</span>() * ((max + <span class="number">1</span>) - min))</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>40.将阿拉伯数字翻译成中文的大写数字</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">numberToChinese</span> = (<span class="params">num</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">var</span> <span class="variable constant_">AA</span> = <span class="keyword">new</span> <span class="title class_">Array</span>(<span class="string">&quot;零&quot;</span>, <span class="string">&quot;一&quot;</span>, <span class="string">&quot;二&quot;</span>, <span class="string">&quot;三&quot;</span>, <span class="string">&quot;四&quot;</span>, <span class="string">&quot;五&quot;</span>, <span class="string">&quot;六&quot;</span>, <span class="string">&quot;七&quot;</span>, <span class="string">&quot;八&quot;</span>, <span class="string">&quot;九&quot;</span>, <span class="string">&quot;十&quot;</span>);</span><br><span class="line">    <span class="keyword">var</span> <span class="variable constant_">BB</span> = <span class="keyword">new</span> <span class="title class_">Array</span>(<span class="string">&quot;&quot;</span>, <span class="string">&quot;十&quot;</span>, <span class="string">&quot;百&quot;</span>, <span class="string">&quot;仟&quot;</span>, <span class="string">&quot;萬&quot;</span>, <span class="string">&quot;億&quot;</span>, <span class="string">&quot;点&quot;</span>, <span class="string">&quot;&quot;</span>);</span><br><span class="line">    <span class="keyword">var</span> a = (<span class="string">&quot;&quot;</span> + num).<span class="title function_">replace</span>(<span class="regexp">/(^0*)/g</span>, <span class="string">&quot;&quot;</span>).<span class="title function_">split</span>(<span class="string">&quot;.&quot;</span>),</span><br><span class="line">        k = <span class="number">0</span>,</span><br><span class="line">        re = <span class="string">&quot;&quot;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = a[<span class="number">0</span>].<span class="property">length</span> - <span class="number">1</span>; i &gt;= <span class="number">0</span>; i--) &#123;</span><br><span class="line">        <span class="keyword">switch</span> (k) &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">0</span>:</span><br><span class="line">                re = <span class="variable constant_">BB</span>[<span class="number">7</span>] + re;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">4</span>:</span><br><span class="line">                <span class="keyword">if</span> (!<span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&quot;0&#123;4&#125;//d&#123;&quot;</span> + (a[<span class="number">0</span>].<span class="property">length</span> - i - <span class="number">1</span>) + <span class="string">&quot;&#125;$&quot;</span>)</span><br><span class="line">                    .<span class="title function_">test</span>(a[<span class="number">0</span>]))</span><br><span class="line">                    re = <span class="variable constant_">BB</span>[<span class="number">4</span>] + re;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">8</span>:</span><br><span class="line">                re = <span class="variable constant_">BB</span>[<span class="number">5</span>] + re;</span><br><span class="line">                <span class="variable constant_">BB</span>[<span class="number">7</span>] = <span class="variable constant_">BB</span>[<span class="number">5</span>];</span><br><span class="line">                k = <span class="number">0</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (k % <span class="number">4</span> == <span class="number">2</span> &amp;&amp; a[<span class="number">0</span>].<span class="title function_">charAt</span>(i + <span class="number">2</span>) != <span class="number">0</span> &amp;&amp; a[<span class="number">0</span>].<span class="title function_">charAt</span>(i + <span class="number">1</span>) == <span class="number">0</span>)</span><br><span class="line">            re = <span class="variable constant_">AA</span>[<span class="number">0</span>] + re;</span><br><span class="line">        <span class="keyword">if</span> (a[<span class="number">0</span>].<span class="title function_">charAt</span>(i) != <span class="number">0</span>)</span><br><span class="line">            re = <span class="variable constant_">AA</span>[a[<span class="number">0</span>].<span class="title function_">charAt</span>(i)] + <span class="variable constant_">BB</span>[k % <span class="number">4</span>] + re;</span><br><span class="line">        k++;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (a.<span class="property">length</span> &gt; <span class="number">1</span>) <span class="comment">// 加上小数部分(如果有小数部分)</span></span><br><span class="line">    &#123;</span><br><span class="line">        re += <span class="variable constant_">BB</span>[<span class="number">6</span>];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; a[<span class="number">1</span>].<span class="property">length</span>; i++)</span><br><span class="line">            re += <span class="variable constant_">AA</span>[a[<span class="number">1</span>].<span class="title function_">charAt</span>(i)];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (re == <span class="string">&#x27;一十&#x27;</span>)</span><br><span class="line">        re = <span class="string">&quot;十&quot;</span>;</span><br><span class="line">    <span class="keyword">if</span> (re.<span class="title function_">match</span>(<span class="regexp">/^一/</span>) &amp;&amp; re.<span class="property">length</span> == <span class="number">3</span>)</span><br><span class="line">        re = re.<span class="title function_">replace</span>(<span class="string">&quot;一&quot;</span>, <span class="string">&quot;&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> re;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>41.将数字转换为大写金额</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">changeToChinese</span> = (<span class="params">Num</span>) =&gt; &#123;</span><br><span class="line">    <span class="comment">//判断如果传递进来的不是字符的话转换为字符</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="title class_">Num</span> == <span class="string">&quot;number&quot;</span>) &#123;</span><br><span class="line">        <span class="title class_">Num</span> = <span class="keyword">new</span> <span class="title class_">String</span>(<span class="title class_">Num</span>);</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="title class_">Num</span> = <span class="title class_">Num</span>.<span class="title function_">replace</span>(<span class="regexp">/,/g</span>, <span class="string">&quot;&quot;</span>) <span class="comment">//替换tomoney()中的“,”</span></span><br><span class="line">    <span class="title class_">Num</span> = <span class="title class_">Num</span>.<span class="title function_">replace</span>(<span class="regexp">/ /g</span>, <span class="string">&quot;&quot;</span>) <span class="comment">//替换tomoney()中的空格</span></span><br><span class="line">    <span class="title class_">Num</span> = <span class="title class_">Num</span>.<span class="title function_">replace</span>(<span class="regexp">/￥/g</span>, <span class="string">&quot;&quot;</span>) <span class="comment">//替换掉可能出现的￥字符</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">isNaN</span>(<span class="title class_">Num</span>)) &#123; <span class="comment">//验证输入的字符是否为数字</span></span><br><span class="line">        <span class="comment">//alert(&quot;请检查小写金额是否正确&quot;);</span></span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;&quot;</span>;</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="comment">//字符处理完毕后开始转换，采用前后两部分分别转换</span></span><br><span class="line">    <span class="keyword">var</span> part = <span class="title class_">String</span>(<span class="title class_">Num</span>).<span class="title function_">split</span>(<span class="string">&quot;.&quot;</span>);</span><br><span class="line">    <span class="keyword">var</span> newchar = <span class="string">&quot;&quot;</span>;</span><br><span class="line">    <span class="comment">//小数点前进行转化</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = part[<span class="number">0</span>].<span class="property">length</span> - <span class="number">1</span>; i &gt;= <span class="number">0</span>; i--) &#123;</span><br><span class="line">        <span class="keyword">if</span> (part[<span class="number">0</span>].<span class="property">length</span> &gt; <span class="number">10</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="string">&quot;&quot;</span>;</span><br><span class="line">            <span class="comment">//若数量超过拾亿单位，提示</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">var</span> tmpnewchar = <span class="string">&quot;&quot;</span></span><br><span class="line">        <span class="keyword">var</span> perchar = part[<span class="number">0</span>].<span class="title function_">charAt</span>(i);</span><br><span class="line">        <span class="keyword">switch</span> (perchar) &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&quot;0&quot;</span>:</span><br><span class="line">                tmpnewchar = <span class="string">&quot;零&quot;</span> + tmpnewchar;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&quot;1&quot;</span>:</span><br><span class="line">                tmpnewchar = <span class="string">&quot;壹&quot;</span> + tmpnewchar;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&quot;2&quot;</span>:</span><br><span class="line">                tmpnewchar = <span class="string">&quot;贰&quot;</span> + tmpnewchar;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&quot;3&quot;</span>:</span><br><span class="line">                tmpnewchar = <span class="string">&quot;叁&quot;</span> + tmpnewchar;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&quot;4&quot;</span>:</span><br><span class="line">                tmpnewchar = <span class="string">&quot;肆&quot;</span> + tmpnewchar;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&quot;5&quot;</span>:</span><br><span class="line">                tmpnewchar = <span class="string">&quot;伍&quot;</span> + tmpnewchar;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&quot;6&quot;</span>:</span><br><span class="line">                tmpnewchar = <span class="string">&quot;陆&quot;</span> + tmpnewchar;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&quot;7&quot;</span>:</span><br><span class="line">                tmpnewchar = <span class="string">&quot;柒&quot;</span> + tmpnewchar;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&quot;8&quot;</span>:</span><br><span class="line">                tmpnewchar = <span class="string">&quot;捌&quot;</span> + tmpnewchar;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&quot;9&quot;</span>:</span><br><span class="line">                tmpnewchar = <span class="string">&quot;玖&quot;</span> + tmpnewchar;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">switch</span> (part[<span class="number">0</span>].<span class="property">length</span> - i - <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">0</span>:</span><br><span class="line">                tmpnewchar = tmpnewchar + <span class="string">&quot;元&quot;</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">                <span class="keyword">if</span> (perchar != <span class="number">0</span>) tmpnewchar = tmpnewchar + <span class="string">&quot;拾&quot;</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">                <span class="keyword">if</span> (perchar != <span class="number">0</span>) tmpnewchar = tmpnewchar + <span class="string">&quot;佰&quot;</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line">                <span class="keyword">if</span> (perchar != <span class="number">0</span>) tmpnewchar = tmpnewchar + <span class="string">&quot;仟&quot;</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">4</span>:</span><br><span class="line">                tmpnewchar = tmpnewchar + <span class="string">&quot;万&quot;</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">5</span>:</span><br><span class="line">                <span class="keyword">if</span> (perchar != <span class="number">0</span>) tmpnewchar = tmpnewchar + <span class="string">&quot;拾&quot;</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">6</span>:</span><br><span class="line">                <span class="keyword">if</span> (perchar != <span class="number">0</span>) tmpnewchar = tmpnewchar + <span class="string">&quot;佰&quot;</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">7</span>:</span><br><span class="line">                <span class="keyword">if</span> (perchar != <span class="number">0</span>) tmpnewchar = tmpnewchar + <span class="string">&quot;仟&quot;</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">8</span>:</span><br><span class="line">                tmpnewchar = tmpnewchar + <span class="string">&quot;亿&quot;</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">9</span>:</span><br><span class="line">                tmpnewchar = tmpnewchar + <span class="string">&quot;拾&quot;</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">var</span> newchar = tmpnewchar + newchar;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//小数点之后进行转化</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="title class_">Num</span>.<span class="title function_">indexOf</span>(<span class="string">&quot;.&quot;</span>) != -<span class="number">1</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (part[<span class="number">1</span>].<span class="property">length</span> &gt; <span class="number">2</span>) &#123;</span><br><span class="line">            <span class="comment">// alert(&quot;小数点之后只能保留两位,系统将自动截断&quot;);</span></span><br><span class="line">            part[<span class="number">1</span>] = part[<span class="number">1</span>].<span class="title function_">substr</span>(<span class="number">0</span>, <span class="number">2</span>)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; part[<span class="number">1</span>].<span class="property">length</span>; i++) &#123;</span><br><span class="line">            tmpnewchar = <span class="string">&quot;&quot;</span></span><br><span class="line">            perchar = part[<span class="number">1</span>].<span class="title function_">charAt</span>(i)</span><br><span class="line">            <span class="keyword">switch</span> (perchar) &#123;</span><br><span class="line">                <span class="keyword">case</span> <span class="string">&quot;0&quot;</span>:</span><br><span class="line">                    tmpnewchar = <span class="string">&quot;零&quot;</span> + tmpnewchar;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="string">&quot;1&quot;</span>:</span><br><span class="line">                    tmpnewchar = <span class="string">&quot;壹&quot;</span> + tmpnewchar;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="string">&quot;2&quot;</span>:</span><br><span class="line">                    tmpnewchar = <span class="string">&quot;贰&quot;</span> + tmpnewchar;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="string">&quot;3&quot;</span>:</span><br><span class="line">                    tmpnewchar = <span class="string">&quot;叁&quot;</span> + tmpnewchar;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="string">&quot;4&quot;</span>:</span><br><span class="line">                    tmpnewchar = <span class="string">&quot;肆&quot;</span> + tmpnewchar;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="string">&quot;5&quot;</span>:</span><br><span class="line">                    tmpnewchar = <span class="string">&quot;伍&quot;</span> + tmpnewchar;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="string">&quot;6&quot;</span>:</span><br><span class="line">                    tmpnewchar = <span class="string">&quot;陆&quot;</span> + tmpnewchar;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="string">&quot;7&quot;</span>:</span><br><span class="line">                    tmpnewchar = <span class="string">&quot;柒&quot;</span> + tmpnewchar;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="string">&quot;8&quot;</span>:</span><br><span class="line">                    tmpnewchar = <span class="string">&quot;捌&quot;</span> + tmpnewchar;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="string">&quot;9&quot;</span>:</span><br><span class="line">                    tmpnewchar = <span class="string">&quot;玖&quot;</span> + tmpnewchar;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (i == <span class="number">0</span>) tmpnewchar = tmpnewchar + <span class="string">&quot;角&quot;</span>;</span><br><span class="line">            <span class="keyword">if</span> (i == <span class="number">1</span>) tmpnewchar = tmpnewchar + <span class="string">&quot;分&quot;</span>;</span><br><span class="line">            newchar = newchar + tmpnewchar;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//替换所有无用汉字</span></span><br><span class="line">    <span class="keyword">while</span> (newchar.<span class="title function_">search</span>(<span class="string">&quot;零零&quot;</span>) != -<span class="number">1</span>)</span><br><span class="line">        newchar = newchar.<span class="title function_">replace</span>(<span class="string">&quot;零零&quot;</span>, <span class="string">&quot;零&quot;</span>);</span><br><span class="line">    newchar = newchar.<span class="title function_">replace</span>(<span class="string">&quot;零亿&quot;</span>, <span class="string">&quot;亿&quot;</span>);</span><br><span class="line">    newchar = newchar.<span class="title function_">replace</span>(<span class="string">&quot;亿万&quot;</span>, <span class="string">&quot;亿&quot;</span>);</span><br><span class="line">    newchar = newchar.<span class="title function_">replace</span>(<span class="string">&quot;零万&quot;</span>, <span class="string">&quot;万&quot;</span>);</span><br><span class="line">    newchar = newchar.<span class="title function_">replace</span>(<span class="string">&quot;零元&quot;</span>, <span class="string">&quot;元&quot;</span>);</span><br><span class="line">    newchar = newchar.<span class="title function_">replace</span>(<span class="string">&quot;零角&quot;</span>, <span class="string">&quot;&quot;</span>);</span><br><span class="line">    newchar = newchar.<span class="title function_">replace</span>(<span class="string">&quot;零分&quot;</span>, <span class="string">&quot;&quot;</span>);</span><br><span class="line">    <span class="keyword">if</span> (newchar.<span class="title function_">charAt</span>(newchar.<span class="property">length</span> - <span class="number">1</span>) == <span class="string">&quot;元&quot;</span>) &#123;</span><br><span class="line">        newchar = newchar + <span class="string">&quot;整&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> newchar;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>42.判断一个元素是否在数组中</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">contains</span> = (<span class="params">arr, val</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> arr.<span class="title function_">indexOf</span>(val) != -<span class="number">1</span> ? <span class="literal">true</span> : <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>43.数组排序，{type} 1：从小到大 2：从大到小 3：随机</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">sort</span> = (<span class="params">arr, type = <span class="number">1</span></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> arr.<span class="title function_">sort</span>(<span class="function">(<span class="params">a, b</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">switch</span> (type) &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">                <span class="keyword">return</span> a - b;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">                <span class="keyword">return</span> b - a;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line">                <span class="keyword">return</span> <span class="title class_">Math</span>.<span class="title function_">random</span>() - <span class="number">0.5</span>;</span><br><span class="line">            <span class="attr">default</span>:</span><br><span class="line">                <span class="keyword">return</span> arr;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>44.去重</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">unique</span> = (<span class="params">arr</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="title class_">Array</span>.<span class="title function_">hasOwnProperty</span>(<span class="string">&#x27;from&#x27;</span>)) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="title class_">Array</span>.<span class="title function_">from</span>(<span class="keyword">new</span> <span class="title class_">Set</span>(arr));</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">var</span> n = &#123;&#125;, r = [];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; arr.<span class="property">length</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (!n[arr[i]]) &#123;</span><br><span class="line">                n[arr[i]] = <span class="literal">true</span>;</span><br><span class="line">                r.<span class="title function_">push</span>(arr[i]);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> r;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>45.求两个集合的并集</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">union</span> = (<span class="params">a, b</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">var</span> newArr = a.<span class="title function_">concat</span>(b);</span><br><span class="line">    <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="title function_">unique</span>(newArr);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>46.求两个集合的交集</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">intersect</span> = (<span class="params">a, b</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">var</span> _this = <span class="variable language_">this</span>;</span><br><span class="line">    a = <span class="variable language_">this</span>.<span class="title function_">unique</span>(a);</span><br><span class="line">    <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="title function_">map</span>(a, <span class="keyword">function</span> (<span class="params">o</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> _this.<span class="title function_">contains</span>(b, o) ? o : <span class="literal">null</span>;</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>47.删除其中一个元素</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">remove</span> = (<span class="params">arr, ele</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">var</span> index = arr.<span class="title function_">indexOf</span>(ele);</span><br><span class="line">    <span class="keyword">if</span> (index &gt; -<span class="number">1</span>) &#123;</span><br><span class="line">        arr.<span class="title function_">splice</span>(index, <span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> arr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>48.将类数组转换为数组</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">formArray</span> = (<span class="params">ary</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">var</span> arr = [];</span><br><span class="line">    <span class="keyword">if</span> (<span class="title class_">Array</span>.<span class="title function_">isArray</span>(ary)) &#123;</span><br><span class="line">        arr = ary;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        arr = <span class="title class_">Array</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">slice</span>.<span class="title function_">call</span>(ary);</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">return</span> arr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>49.最大值</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">max</span> = (<span class="params">arr</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Math</span>.<span class="property">max</span>.<span class="title function_">apply</span>(<span class="literal">null</span>, arr);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>50.最小值</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">min</span> = (<span class="params">arr</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Math</span>.<span class="property">min</span>.<span class="title function_">apply</span>(<span class="literal">null</span>, arr);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>51.求和</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">sum</span> = (<span class="params">arr</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> arr.<span class="title function_">reduce</span>(<span class="function">(<span class="params">pre, cur</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> pre + cur</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>52.平均值</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">average</span> = (<span class="params">arr</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="title function_">sum</span>(arr) / arr.<span class="property">length</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>53.去除空格,type: 1-所有空格 2-前后空格 3-前空格 4-后空格</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">trim</span> = (<span class="params">str, type</span>) =&gt; &#123;</span><br><span class="line">    type = type || <span class="number">1</span></span><br><span class="line">    <span class="keyword">switch</span> (type) &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">            <span class="keyword">return</span> str.<span class="title function_">replace</span>(<span class="regexp">/\s+/g</span>, <span class="string">&quot;&quot;</span>);</span><br><span class="line">        <span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">            <span class="keyword">return</span> str.<span class="title function_">replace</span>(<span class="regexp">/(^\s*)|(\s*$)/g</span>, <span class="string">&quot;&quot;</span>);</span><br><span class="line">        <span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line">            <span class="keyword">return</span> str.<span class="title function_">replace</span>(<span class="regexp">/(^\s*)/g</span>, <span class="string">&quot;&quot;</span>);</span><br><span class="line">        <span class="keyword">case</span> <span class="number">4</span>:</span><br><span class="line">            <span class="keyword">return</span> str.<span class="title function_">replace</span>(<span class="regexp">/(\s*$)/g</span>, <span class="string">&quot;&quot;</span>);</span><br><span class="line">        <span class="attr">default</span>:</span><br><span class="line">            <span class="keyword">return</span> str;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>54.字符转换，type: 1:首字母大写 2：首字母小写 3：大小写转换 4：全部大写 5：全部小写</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">changeCase</span> = (<span class="params">str, type</span>) =&gt; &#123;</span><br><span class="line">    type = type || <span class="number">4</span></span><br><span class="line">    <span class="keyword">switch</span> (type) &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">            <span class="keyword">return</span> str.<span class="title function_">replace</span>(<span class="regexp">/\b\w+\b/g</span>, <span class="keyword">function</span> (<span class="params">word</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> word.<span class="title function_">substring</span>(<span class="number">0</span>, <span class="number">1</span>).<span class="title function_">toUpperCase</span>() + word.<span class="title function_">substring</span>(<span class="number">1</span>).<span class="title function_">toLowerCase</span>();</span><br><span class="line"></span><br><span class="line">            &#125;);</span><br><span class="line">        <span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">            <span class="keyword">return</span> str.<span class="title function_">replace</span>(<span class="regexp">/\b\w+\b/g</span>, <span class="keyword">function</span> (<span class="params">word</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> word.<span class="title function_">substring</span>(<span class="number">0</span>, <span class="number">1</span>).<span class="title function_">toLowerCase</span>() + word.<span class="title function_">substring</span>(<span class="number">1</span>).<span class="title function_">toUpperCase</span>();</span><br><span class="line">            &#125;);</span><br><span class="line">        <span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line">            <span class="keyword">return</span> str.<span class="title function_">split</span>(<span class="string">&#x27;&#x27;</span>).<span class="title function_">map</span>(<span class="keyword">function</span> (<span class="params">word</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> (<span class="regexp">/[a-z]/</span>.<span class="title function_">test</span>(word)) &#123;</span><br><span class="line">                    <span class="keyword">return</span> word.<span class="title function_">toUpperCase</span>();</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="keyword">return</span> word.<span class="title function_">toLowerCase</span>()</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;).<span class="title function_">join</span>(<span class="string">&#x27;&#x27;</span>)</span><br><span class="line">        <span class="keyword">case</span> <span class="number">4</span>:</span><br><span class="line">            <span class="keyword">return</span> str.<span class="title function_">toUpperCase</span>();</span><br><span class="line">        <span class="keyword">case</span> <span class="number">5</span>:</span><br><span class="line">            <span class="keyword">return</span> str.<span class="title function_">toLowerCase</span>();</span><br><span class="line">        <span class="attr">default</span>:</span><br><span class="line">            <span class="keyword">return</span> str;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>55.检测密码强度</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">checkPwd</span> = (<span class="params">str</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">var</span> <span class="title class_">Lv</span> = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span> (str.<span class="property">length</span> &lt; <span class="number">6</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="title class_">Lv</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (<span class="regexp">/[0-9]/</span>.<span class="title function_">test</span>(str)) &#123;</span><br><span class="line">        <span class="title class_">Lv</span>++</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (<span class="regexp">/[a-z]/</span>.<span class="title function_">test</span>(str)) &#123;</span><br><span class="line">        <span class="title class_">Lv</span>++</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (<span class="regexp">/[A-Z]/</span>.<span class="title function_">test</span>(str)) &#123;</span><br><span class="line">        <span class="title class_">Lv</span>++</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (<span class="regexp">/[\.|-|_]/</span>.<span class="title function_">test</span>(str)) &#123;</span><br><span class="line">        <span class="title class_">Lv</span>++</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Lv</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>56.函数节流器</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">debouncer</span> = (<span class="params">fn, time, interval = <span class="number">200</span></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">if</span> (time - (<span class="variable language_">window</span>.<span class="property">debounceTimestamp</span> || <span class="number">0</span>) &gt; interval) &#123;</span><br><span class="line">        fn &amp;&amp; <span class="title function_">fn</span>();</span><br><span class="line">        <span class="variable language_">window</span>.<span class="property">debounceTimestamp</span> = time;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>57.在字符串中插入新字符串</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">insertStr</span> = (<span class="params">soure, index, newStr</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">var</span> str = soure.<span class="title function_">slice</span>(<span class="number">0</span>, index) + newStr + soure.<span class="title function_">slice</span>(index);</span><br><span class="line">    <span class="keyword">return</span> str;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>58.判断两个对象是否键值相同</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isObjectEqual</span> = (<span class="params">a, b</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">var</span> aProps = <span class="title class_">Object</span>.<span class="title function_">getOwnPropertyNames</span>(a);</span><br><span class="line">    <span class="keyword">var</span> bProps = <span class="title class_">Object</span>.<span class="title function_">getOwnPropertyNames</span>(b);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (aProps.<span class="property">length</span> !== bProps.<span class="property">length</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; aProps.<span class="property">length</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">var</span> propName = aProps[i];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (a[propName] !== b[propName]) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>59.16进制颜色转RGBRGBA字符串</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">colorToRGB</span> = (<span class="params">val, opa</span>) =&gt; &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> pattern = <span class="regexp">/^(#?)[a-fA-F0-9]&#123;6&#125;$/</span>; <span class="comment">//16进制颜色值校验规则</span></span><br><span class="line">    <span class="keyword">var</span> isOpa = <span class="keyword">typeof</span> opa == <span class="string">&#x27;number&#x27;</span>; <span class="comment">//判断是否有设置不透明度</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (!pattern.<span class="title function_">test</span>(val)) &#123; <span class="comment">//如果值不符合规则返回空字符</span></span><br><span class="line">        <span class="keyword">return</span> <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> v = val.<span class="title function_">replace</span>(<span class="regexp">/#/</span>, <span class="string">&#x27;&#x27;</span>); <span class="comment">//如果有#号先去除#号</span></span><br><span class="line">    <span class="keyword">var</span> rgbArr = [];</span><br><span class="line">    <span class="keyword">var</span> rgbStr = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">var</span> item = v.<span class="title function_">substring</span>(i * <span class="number">2</span>, i * <span class="number">2</span> + <span class="number">2</span>);</span><br><span class="line">        <span class="keyword">var</span> num = <span class="built_in">parseInt</span>(item, <span class="number">16</span>);</span><br><span class="line">        rgbArr.<span class="title function_">push</span>(num);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    rgbStr = rgbArr.<span class="title function_">join</span>();</span><br><span class="line">    rgbStr = <span class="string">&#x27;rgb&#x27;</span> + (isOpa ? <span class="string">&#x27;a&#x27;</span> : <span class="string">&#x27;&#x27;</span>) + <span class="string">&#x27;(&#x27;</span> + rgbStr + (isOpa ? <span class="string">&#x27;,&#x27;</span> + opa : <span class="string">&#x27;&#x27;</span>) + <span class="string">&#x27;)&#x27;</span>;</span><br><span class="line">    <span class="keyword">return</span> rgbStr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>60.追加url参数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">appendQuery</span> = (<span class="params">url, key, value</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">var</span> options = key;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> options == <span class="string">&#x27;string&#x27;</span>) &#123;</span><br><span class="line">        options = &#123;&#125;;</span><br><span class="line">        options[key] = value;</span><br><span class="line">    &#125;</span><br><span class="line">    options = $.<span class="title function_">param</span>(options);</span><br><span class="line">    <span class="keyword">if</span> (url.<span class="title function_">includes</span>(<span class="string">&#x27;?&#x27;</span>)) &#123;</span><br><span class="line">        url += <span class="string">&#x27;&amp;&#x27;</span> + options</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        url += <span class="string">&#x27;?&#x27;</span> + options</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> url;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://segmentfault.com/a/1190000022736837?utm_source=weekly&utm_medium=email&utm_campaign=SegmentFault%20%E7%B2%BE%E9%80%89%E6%AF%8F%E5%91%A8%E7%B2%BE%E9%80%89%E4%B8%A8%E5%89%8D%E7%AB%AF%E5%B8%B8%E7%94%A860+%20%E7%A7%8D%E5%B7%A5%E5%85%B7%E6%96%B9%E6%B3%95%E4%B8%A8SegmentFault%20%E7%8B%AC%E7%AB%8B%E5%BC%80%E5%8F%91%E8%80%85%E6%94%AF%E6%8C%81%E8%AE%A1%E5%88%92%E5%BC%80%E5%90%AF%EF%BC%8C%E5%B8%AE%E5%8A%A9%E7%8B%AC%E7%AB%8B%E5%BC%80%E5%8F%91%E8%80%85%E5%BB%BA%E7%AB%8B%E5%BD%B1%E5%93%8D%E5%8A%9B%EF%BC%81">来源</a></p>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Wyatex
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://wyatex.online/%E5%89%8D%E7%AB%AF/%E5%89%8D%E7%AB%AF%E5%B8%B8%E7%94%A860%E4%BD%99%E7%A7%8D%E5%B7%A5%E5%85%B7%E6%96%B9%E6%B3%95/" title="JS常用60余种工具方法">https://wyatex.online/前端/前端常用60余种工具方法/</a>
  </li>
  <li class="post-copyright-license">
      <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/tags/JavaScript/" rel="tag"># JavaScript</a>
              <a href="/tags/%E5%89%8D%E7%AB%AF/" rel="tag"># 前端</a>
              <a href="/tags/%E5%B7%A5%E5%85%B7/" rel="tag"># 工具</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/C%E8%AF%AD%E8%A8%80/C%E8%AF%AD%E8%A8%80%E7%9A%84%E8%B0%83%E8%AF%95%E6%96%B9%E6%B3%95/" rel="prev" title="C语言的调试方法">
                  <i class="fa fa-angle-left"></i> C语言的调试方法
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/%E6%95%B0%E7%A0%81%E4%BA%A7%E5%93%81%E6%8E%A8%E8%8D%90/%E8%87%AA%E5%8A%A8%E5%8C%96%E5%88%B7%E8%A7%86%E9%A2%91-%E7%AD%94%E9%A2%98-%E8%80%83%E8%AF%95-%E4%BD%8E%E8%B0%83%E4%BD%BF%E7%94%A8%E5%88%AB%E4%B9%B1%E4%BC%A0/" rel="next" title="自动化刷视频/答题/考试[低调使用别乱传]">
                  自动化刷视频/答题/考试[低调使用别乱传] <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2019 – 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Wyatex</span>
  </div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script size="300" alpha="0.6" zIndex="-1" src="/lib/ribbon.js/dist/ribbon.min.js"></script>
  <script src="/lib/animejs/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="/lib/@next-theme/pjax/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="/lib/@fancyapps/ui/dist/fancybox/fancybox.umd.js" integrity="sha256-q8XkJ6dj5VwSvzI8+nATCHHQG+Xv/dAZBCgqmu93zOY=" crossorigin="anonymous"></script>
  <script src="/lib/lozad/dist/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="/lib/hexo-generator-searchdb/dist/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>



  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





</body>
</html>
